package com.example.graalvm21.config.db;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * <p>设置切面</p>
 *
 * @author wz
 *
 * @date 2025/10/27 9:29
 *
 * @version 1.0
 */
@Component
@Order(value = -100)
@Slf4j
@Aspect
public class DataSourceSwitchAspect {

    @Pointcut("execution(* com.example.graalvm21.mapper.master..*.*(..))")
    private void masterAspect() {
    }

    @Pointcut("execution(* com.example.graalvm21.mapper.slave1..*.*(..))")
    private void slave1Aspect() {
    }


    @Before("masterAspect()")
    public void master() {
        log.info("切换到检测 数据源...");
        DbContextHolder.setDbType(DBTypeEnum.master);
    }

    @Before("slave1Aspect()")
    public void slave1() {
        log.info("切换到检验 数据源...");
        DbContextHolder.setDbType(DBTypeEnum.slave1);
    }

    @After("masterAspect()")
    public void masterClear() {
        log.info("清除检测 数据源...");
        DbContextHolder.clearDbType();
    }

    @After("slave1Aspect()")
    public void slave1Clear() {
        log.info("清除检验 数据源...");
        DbContextHolder.clearDbType();
    }

}
